package com.zzstm.msmservice.controller;

import com.zzstm.commonUtils.R;
import com.zzstm.msmservice.service.MsmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/edumsm")
@CrossOrigin
public class MsmController {

    @Autowired
    private MsmService msmService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @GetMapping("/sendCode/{phone}")
    public R getCode(@PathVariable String phone){
        System.err.println(phone);
        String code = redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(code)){
            return R.ok();
        }else {
            code = msmService.sendCode(phone);
            if (code == null){
                return R.error().message("短息发送失败");
            }else {
                redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
                return R.ok();
            }
        }
//        return R.ok();
    }

}
